//
//  ConsoleBattleship.java
//  NetworkBattleship
//
//  Created by Alan Joyce on 1/14/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//
package gamefiles;

import java.io.*;

public class ConsoleBattleship extends Object
{
	//Declare variables.
	boolean isServer;
	boolean promptLoop;
	String serverString;
	String ipAddress;
	
	BattleshipNetworkCom netCom;
	InputStreamReader isr;
	BufferedReader input;
	
	public ConsoleBattleship() {
		isServer = false;
		promptLoop = true;
		serverString = "";
		ipAddress = "";
		
		netCom = new BattleshipNetworkCom();
		isr = new InputStreamReader(System.in);
		input = new BufferedReader(isr);
	}
	
	public void init() {
		while(promptLoop) {
			System.out.println("Is this a server (Y/N)?");
			
			try {
				serverString = input.readLine();
			}catch(IOException e) {
				System.out.println("IO Exception: " + e);
			}
			
			if(serverString.toLowerCase().equals("y")) {
				isServer = true;
				promptLoop = false;
			}
			else if(serverString.toLowerCase().equals("n")) {
				isServer = false;
				promptLoop = false;
			}
			else {
				System.out.println("Invalid input. Try again.");
			}
		}
		
		if(isServer == false) {
			System.out.println("What is the IP address of the server?");
			
			try {
				ipAddress = input.readLine();
			}catch(IOException e) {
				System.out.println("IO Exception: " + e);
			}
		}
		
		if(isServer == true) {
			netCom.hostGame();
		}
		else {
			netCom.connectGame(ipAddress);
		}
	}
}
